package com.zhangxx.inventory.thread.request;
import com.zhangxx.inventory.pojo.ProductInventory;
import com.zhangxx.inventory.service.ProductInventoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * @ClassName ProductInventoryCacheRefreshRequest
 * @Description 查询商品库存信息数据 并保存商品库存信息到redis
 * @Author leizhang
 * Date 2020/9/16 9:02 下午
 * @Version 1.0
 **/
public class ProductInventoryCacheRefreshRequest implements Request{
    private Logger logger = LoggerFactory.getLogger(ProductInventoryCacheRefreshRequest.class);
    /**
     * @Description : 商品
     **/
    private ProductInventory  productInventory;

    private Boolean forceRefresh;
    /**
     * @Description : 商品库存Service
     **/
    private ProductInventoryService productInventoryService;

    public ProductInventoryCacheRefreshRequest(ProductInventory  productInventory,
                                               ProductInventoryService productInventoryService,
                                               boolean forceRefresh) {
        this.productInventory = productInventory;
        this.productInventoryService = productInventoryService;
        this.forceRefresh = forceRefresh;
    }

    /**
     * @Description : 查询库存 1.查询数据库信息 2.更新redis缓存
     * @Author : leizhang
     * @Date 16:29 2022/3/18
     * @param
     * @return void
     **/
    @Override
    public void process() {
        //从数据库中查询最新的商品库存数量
        try {
            ProductInventory productInventory = productInventoryService.findProductInventory(this.productInventory);
            if (productInventory != null) {
                productInventoryService.setProductInventoryCache(productInventory);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }

    @Override
    public ProductInventory getProductInfo() {
        return productInventory;
    }

    @Override
    public boolean forceRefresh() {
        return forceRefresh;
    }
}
